/**
 * https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
 * 最大深度
 * @param root 
 */
const maxDepth = (root: TreeNode | null): number => {
    if (root === null) return 0;

    // 保存根节点
    let count = 1,
        level = 1;
    const arr: TreeNode[] = [root]

    while (arr.length > 0) {
        // 弹出最后一项
        const n = arr.pop()!;
        count--;

        if (n.left !== null) {
            arr.push(n.left)
        }

        if (n.right !== null) {
            arr.push(n.right);
        }

        // 减到0了
        if (count === 0) {
            count = arr.length;
            level++
        }
    }

    return level 
};